package com.yuneec.droneservice.command;

import com.yuneec.droneservice.request.IResponse;
import com.yuneec.droneservice.utils.ByteUtils;

import java.io.UnsupportedEncodingException;
import java.util.Arrays;

/**
 * Created by gjw on 2017/3/27.
 */

public class GetControllerType extends BaseCommand {

    public GetControllerType() {
        super(MAV_RC_COMMAND.MAV_RC_CMD_GET_CONTROL_TYPE);
    }

    @Override
    public IResponse<String> toResponse(byte[] data, int off, int payloadLen) {
        int code = data[off + 1] & 0xff;
        IResponse<String> response = new IResponse(ids(), code);
        if (code == MAV_RC_ERROR_CODE.MAV_RC_ERR_SUCCESS) {
            int len = ByteUtils.byteArrayToShort(Arrays.copyOfRange(data, off + 2, off + 4), IS_BIGENDIAN);
            if (len > 0) {
                try {
                    String type = new String(Arrays.copyOfRange(data, off + 4, off + 4 + len), "utf8");
                    response.setParam(type);
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
        }
        return response;
    }

}
